package com.example.st.controller;

import com.example.st.common.ResponseResult;
import com.example.st.entity.User;
import com.example.st.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/getAll/{uid}")
    public ResponseResult getAllUsers(@PathVariable("uid") Long requesterUid) {
        ResponseResult<List<User>> response = userService.getAllUsers(requesterUid);
        return response;
    }

    @GetMapping("/get/{uid}")
    public ResponseResult getUserById(@PathVariable("uid") Long requesterUid, @RequestParam("targetUid") Long targetUid) {
        ResponseResult<User> response = userService.getUserById(requesterUid, targetUid);
        return response;
    }

    @PostMapping("/add/{uid}")
    public ResponseResult createUser(@PathVariable("uid") Long requesterUid, @RequestBody User user) {
        ResponseResult<User> response = userService.createUser(requesterUid, user);
        return response;
    }

    @PutMapping("/put/{uid}")
    public ResponseResult updateUser(@PathVariable("uid") Long requesterUid, @RequestParam("targetUid") Long targetUid, @RequestBody User userDetails) {
        ResponseResult<User> response = userService.updateUser(requesterUid, targetUid, userDetails);
        return response;
    }

    @DeleteMapping("/del/{uid}")
    public ResponseResult deleteUser(@PathVariable("uid") Long requesterUid, @RequestParam("targetUid") Long targetUid) {
        ResponseResult<Void> response = userService.deleteUser(requesterUid, targetUid);
        return response;
    }

    @PostMapping("/register")
    public ResponseResult registerUser(@RequestBody User user) {
        ResponseResult<Void> response = userService.registerUser(user);
        return response;
    }

    @PostMapping("/login")
    public ResponseResult loginUser(@RequestParam String account, @RequestParam String password) {
        ResponseResult<Long> response = userService.authenticateUser(account, password);
        return response;
    }
}

